{% extends '_base_create_update.html' %}

{% block top_css %}
    {{ super() }}
    <!--bees-->
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/bees.css') }}">
{% endblock %}


<!-- Content head -->
{% block head_title %}
    系统管理
{% endblock %}

{% block head_nav %}
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-home"></i></a></li>
        <li><a href="#">权限管理</a></li>
        <li class="active">用户管理</li>
    </ol>
{% endblock %}

<!-- Content body -->
{% block body_title %}
    修改用户信息
{% endblock %}

{% block body_form %}
    <form name="userForm" action="" class="form-horizontal">
        <div class="panel-body">
            <p class="bord-btm pad-ver text-main text-bold">用户信息</p>

            <div class="row">
                <div class="col-lg-12">
                    <fieldset class="pad-top">
                        <div class="form-group">
                            <label class="col-lg-2 control-label"><span class="text-danger">* </span>用户姓名</label>
                            <div class="col-lg-8">
                                <input type="text" class="form-control" name="name" value="{{ name }}" placeholder="请填写用户姓名" data-rule="用户姓名: required;" autocomplete="off">
                            </div>
                        </div>
                    </fieldset>
                </div>
                <div class="col-lg-12">
                     <fieldset class="pad-top">
                        <div class="form-group">
                            <label class="col-lg-2 control-label"><span class="text-danger">* </span>用户名称</label>
                            <div class="col-lg-8">
                                <input type="text" class="form-control" name="username" value="{{ username }}" placeholder="请填写用户名称" data-rule="用户名称: required;" autocomplete="off">
                            </div>
                        </div>
                    </fieldset>
                </div>
                <div class="col-lg-12">
                    <fieldset class="pad-top">
                        <div class="form-group">
                            <label class="col-lg-2 control-label"><span class="text-danger">* </span>用户角色</label>
                            <div class="col-lg-8">
                                <select class="select2 form-control" name="roles" data-placeholder="请选择用户角色">
                                </select>
                            </div>
                        </div>
                    </fieldset>
                </div>
                <div class="col-lg-12">
                   <fieldset class="pad-top">
                        <div class="form-group">
                            <label class="col-lg-2 control-label"><span class="text-danger">* </span>用户邮箱</label>
                            <div class="col-lg-8">
                                <input type="text" class="form-control" name="email" value="{{ email }}" placeholder="请填写员工邮箱" data-rule="员工邮箱: required;" autocomplete="off">
                            </div>
                        </div>
                    </fieldset>
                </div>
                <div class="col-lg-12">
                     <fieldset class="pad-top">
                        <div class="form-group">
                            <label class="col-lg-2 control-label"><span class="text-danger">* </span>手机号码</label>
                            <div class="col-lg-8">
                                <input type="text" class="form-control" name="phone" value="{{ phone }}" placeholder="请填写员工手机号码" data-rule="手机号码: required;" autocomplete="off">
                            </div>
                        </div>
                    </fieldset>
                </div>
                <div class="col-lg-12">
                    <fieldset class="pad-top">
                        <div class="form-group">
                            <label class="col-lg-2 control-label">管理员</label>
                            <div class="col-lg-8">
                                <div class="checkbox">
                                    <input id="radio-menu-show" class="magic-radio" type="radio" name="is_admin" value="1" {% if is_admin %} checked {% endif %}>
                                    <label for="radio-menu-show">&nbsp;是&nbsp;&nbsp;&nbsp;</label>

                                    <input id="radio-menu-none" class="magic-radio" type="radio" name="is_admin" value="0" >
                                    <label for="radio-menu-none">&nbsp;否</label>
                                </div>
                            </div>
                        </div>
                    </fieldset>
                </div>
                <div class="col-lg-12">
                    <fieldset class="pad-top">
                        <div class="form-group">
                            <label class="col-lg-2 control-label"><span class="text-danger">* </span>MFA验证</label>
                            <div class="col-lg-8">
                                <div class="checkbox">
                                    <input id="otp-level-2" class="magic-radio" type="radio" name="otp_level" value="2" {% if otp_level == 2 %} checked {% endif %}>
                                    <label for="otp-level-2">强制</label>

                                    <input id="otp-level-0" class="magic-radio" type="radio" name="otp_level" value="0" {% if otp_level == 0 %} checked {% endif %}>
                                    <label for="otp-level-0">关闭</label>

                                    <input id="otp-level-1" class="magic-radio" type="radio" name="otp_level" value="1" disabled {% if otp_level == 1 %} checked {% endif %}>
                                    <label for="otp-level-1">开启</label>

                                </div>
                            </div>
                        </div>
                    </fieldset>
                </div>
            </div>
        </div>

        <div class="panel-footer">
            <div class="row">
                <div class="col-sm-6 col-sm-offset-6">
                    <div class="col-sm-7 col-sm-offset-3">
                        <button class="btn btn-mint pull-right" type="submit">提交</button>
                    </div>
                </div>
            </div>
        </div>
    </form>
{% endblock %}



{% block buttom_js %}
    {{ super() }}
    <!--bees-->
    <script src="{{ url_for('static', filename='js/bees.js') }}"></script>


    <script type="text/javascript">

        $(function () {
            bees.initServerSideSelect2(
                $('select[name="roles"]'),
                {
                    url: '{{ url_for('perms.api-role-list') }}',
                    selected: '{{ role_ids }}'
                },
                {multiple: true}
            );

            var $formEle = $('form[name="userForm"]');
            bees.niceValidate($formEle);
            $formEle.on("valid.form", function () {
                setAjaxCSRFToken();

                var roles = $('select[name="roles"]').select2('val');
                $.ajax({
                    type: 'post',
                    url: '{{ url_for("users.user-update", pk=default_pk) }}'.replace(bees.defaultPK, '{{ id }}'),
                    data : {
                        'name': $('input[name="name"]').val(),
                        'username': $('input[name="username"]').val(),
                        'roles': roles.join(','),
                        'email': $('input[name="email"]').val(),
                        'phone': $('input[name="phone"]').val(),
                        'is_admin': $('input[name="is_admin"]:checked').val(),
                        'otp_level': $('input[name="otp_level"]:checked').val(),
                        'join_date': $('input[name="join_date"]').val()
                    },
                    success: function (result) {
                        if (result.code != 200) {
                            $formEle.find('*[name='+ result.data.field +']').focus().addClass('has-error');
                            bees.tipError({
                                message: result.data.message
                            });
                        }else {
                            bees.tipInfo({
                                message: '更新成功'
                            });
                            window.location.href = '{{ url_for("users.user-list") }}'
                        }
                    }
                })
            });

        })
    </script>

{% endblock %}
